﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LiteQueryWPF.Controls
{
    /// <summary>
    /// FieldDateComparePanel.xaml 的交互逻辑
    /// </summary>
    public partial class FieldDateComparePanel : UserControl
    {
        public event EventHandler OnValueChanged;

        Entities.Field field;
        public Entities.Field FieldInfo
        {
            get
            {
                if (field == null)
                    field = new Entities.Field();
                if (dtValue.SelectedDate != null)
                    field.Value = LiteQueryWPF.Common.DateHelper.DateToTimestamp((DateTime)dtValue.SelectedDate).ToString();
                field.Operator = ((Entities.Operator)cboOperator.SelectedItem).Value;
                return field;
            }
            set { field = value; }
        }

        public string Caption
        {
            get { return lblCaption.Content.ToString(); }
            set
            {
                if (lblCaption.Dispatcher.Thread!=System.Threading.Thread.CurrentThread)
                    lblCaption.Dispatcher.Invoke(new Action(() => lblCaption.Content = value));
                else
                    lblCaption.Content = value;
            }
        }


        public FieldDateComparePanel()
        {
            InitializeComponent();
            LoadOperators();
        }

        public FieldDateComparePanel(Entities.Field fi)
        {
            InitializeComponent();
            this.FieldInfo = fi;
            this.Caption = fi.Caption;
            LoadOperators();
        }

        private void LoadOperators()
        {
            cboOperator.Items.Add(new Entities.Operator { Caption = "=", Value = "=" });
            cboOperator.Items.Add(new Entities.Operator { Caption = "<>", Value = "<>" });
            cboOperator.Items.Add(new Entities.Operator { Caption = ">", Value = ">" });
            cboOperator.Items.Add(new Entities.Operator { Caption = "<", Value = "<" });
            cboOperator.Items.Add(new Entities.Operator { Caption = ">=", Value = ">=" });
            cboOperator.Items.Add(new Entities.Operator { Caption = "<=", Value = "<=" });
            cboOperator.SelectedIndex = 0;
        }

        private void dtValue_ValueChanged(object sender, EventArgs e)
        {
            if(dtValue.SelectedDate!=null)
                field.Value = LiteQueryWPF.Common.DateHelper.DateToTimestamp((DateTime)dtValue.SelectedDate).ToString();
            field.Operator = ((Entities.Operator)cboOperator.SelectedItem).Value;
            if (OnValueChanged != null)
                OnValueChanged(this, null);
        }

        private void cboOperator_SelectedIndexChanged(object sender, EventArgs e)
        {
            field.Operator = ((Entities.Operator)cboOperator.SelectedItem).Value;
        }
    }
}

